﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WL.Model;

namespace WL.Bll
{
    class ProductService
    {
        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        //public static bool add(DataRow row)
        //{
        //    try
        //    {
        //        Dal.ProductDal.save(row);
        //    }
        //    catch (Exception e)
        //    {
        //        return false;
        //    }
        //    return true;
        //}

        public static DataTable getAll()
        {
            return Dal.ProductDal.getAll();
        }

        public static DataTable getDatas(string key, string upfile)
        {
            return Dal.ProductDal.getDatas(key, upfile);
        }

        public static DataTable addByFields(DataTable dt, Table t)
        {
            
            System.Windows.Forms.MessageBox.Show(t.name);
            return null;
        }
        
       /// <summary>
       /// 添加产品
       /// </summary>
       /// <param name="dt"></param>
       /// <param name="t"></param>
       /// <returns>错误的插入错误的结果</returns>
        internal static DataTable add(DataTable dt, string upfile, Table t)
        {
            
            List<int> list = Dal.ProductDal.save(dt, upfile, t);
            DataTable d = new DataTable();
            foreach (Field f in t.fields)
            {
                d.Columns.Add(f.name);
            }
            
            foreach (var i in list)
            {
                DataRow r = dt.Rows[i];
                DataRow rr = d.NewRow();
                rr.ItemArray = (Object[])r.ItemArray;
                rr[5] = (i+1);
               
                d.Rows.Add(rr);
            }
            return d;
        }
        /*
         /// <summary>
       /// 添加产品
       /// </summary>
       /// <param name="dt"></param>
       /// <param name="t"></param>
       /// <returns>错误的插入错误的结果</returns>
        internal static DataTable add(DataTable dt, string upfile, Table t)
        {
            
            List<int> list = Dal.ProductDal.save(dt, upfile);
            DataTable d = new DataTable();
            
            d.Columns.Add("产品型号");
            d.Columns.Add("库存");
            d.Columns.Add("包装数量");
            d.Columns.Add("供应商");
            d.Columns.Add("价格");
            d.Columns.Add("行数");

            foreach (var i in list)
            {
                DataRow r = dt.Rows[i];
                DataRow rr = d.NewRow();
                rr.ItemArray = (Object[])r.ItemArray;
                rr[5] = (i+1);
               
                d.Rows.Add(rr);
            }
            return d;
        }
         */

        internal static bool update(Product p)
        {
            return Dal.ProductDal.update(p)>0;
            
        }

        internal static bool deleteProductById(string id)
        {
            int iid = 0;
            try {
                iid = Convert.ToInt32(id);

            } catch (Exception e)
            {
                return false;
            }

            if (Dal.ProductDal.delete(iid) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        internal static string[] getFileNams()
        {
            return Dal.ProductDal.getFileNames();
        }

        internal static void deleteProductByUpfile(string upfile)
        {
            Dal.ProductDal.deleteByUpfile(upfile);
        }

        
    }
}
